package com.example.anydo.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.anydo.R;
import com.example.anydo.bean.SubTaskBean;
import com.example.anydo.bean.TaskBean;
import com.example.anydo.util.database.DatabaseHelper;
import com.example.anydo.util.pub_util.MusicPlayer;
import com.example.anydo.util.pub_util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.ViewHolder> {
    private final MusicPlayer player;
    private List<String> data;
    private Activity activity;
    private CheckBox checkBox;
    private int musicPos;

    public MusicAdapter(ArrayList<String> data, Activity activity, int position){
        this.activity = activity;
        this.data = data;
        player = new MusicPlayer(activity);
        this.musicPos = position;
    }

    public int getPosition(){
        return musicPos;
    }

    public void onDestroy(){
        player.onDestroy();
    }
    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(activity).inflate(R.layout.musicitem, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull final ViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(position>0){
                    player.play(position-1);
                }
            }
        });
        if(position == 0){
            holder.textView.setText("静音");
        }else {
            holder.textView.setText(data.get(position-1));

        }
        if(position == musicPos){
            checkBox = holder.checkBox;
            checkBox.setChecked(true);
        }

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    checkBox.setChecked(false);
                    checkBox = holder.checkBox;

                    MusicAdapter.this.musicPos = position;
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size() + 1;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        CheckBox checkBox;
        TextView textView;
        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            this.checkBox = itemView.findViewById(R.id.checkBox);
            this.textView = itemView.findViewById(R.id.textView);
        }
    }
}
